#!/bin/bash

CMOD=$1
NOLOG=$2

CUR=/opt/mes/files/upload/deploy/tmp/$CMOD/mestool/cmd
LOGNAME=updatelog.txt
LOGFILE=$CUR/$LOGNAME

if [ ! -e $CUR ]
then
    exit 1
fi

if [ $NOLOG -a $NOLOG != "nolog" ]
then
    exec 1>$LOGFILE 2>&1
fi

cd $CUR

pre_dir() {
    M=$1

    if [ ! -d $M ]
    then
        mkdir -p $M
        # chown bianhui:bianhui $M
    else
        rm -rf $M
        mkdir -p $M
        # chown bianhui:bianhui $M
    fi
}

# deploy/tmp/mes_x.xx/
cd ../..

MOD=`ls -1`
CUR=`pwd`

for d in $MOD
do
    if [ $d == "server" ]
    then
        # deploy/server/data/
        pre_dir $CUR/../../server/data/
        cp -r server/front $CUR/../../server/data/
    elif [ $d == "mestool" ]
    then
        # deploy/mestool/
        pre_dir $CUR/../../mestool
        cp -r mestool/* $CUR/../../mestool
    elif [ $d == "client" ]
    then
        # deploy/client/data/
        pre_dir $CUR/../../client/data/
        cp -r client/front $CUR/../../client/data/
    fi
done

# deploy/
cd $CUR/../..
CUR=`pwd`

if [ -e $CUR/$LOGNAME ]
then
    rm -f $CUR/$LOGNAME
fi

for d in $MOD
do
    if [ $d == "server" ]
    then
        cd $CUR/server/data/front/
        ./update
    fi
done

for d in $MOD
do
    if [ $d == "client" ]
    then
        cd $CUR/client/data/front/
        ./update
    fi
done

docker images

echo

docker ps

# deploy/tmp/updatelog.txt
if [ $NOLOG -a $NOLOG != "nolog" ]
then
    cp $LOGFILE $CUR/tmp/
fi
